<?php
namespace App\Models\User;

use App\admin\Service\OA;
use App\Jobs\Fun;
use App\Models\Model;
use App\Models\UserQuick;

class UserGongsiModel extends Model
{
    protected $table = 'users_companys_gongsi';
    //


    public function attributeLabels()
    {
        return [
            'id' => 'id',
            'uid' => '用户UID',
            'cate' => ' 弃用     企业类型：1仅工厂，2仅公司，3公司含工厂',
            'facType' => '工厂类型',
            'legal_name' => '法人姓名',
            'legal_sex' => '法人性别',
            'legal_mobile' => '法人常用手机号码',
            'legal_idcard' => '法人身份证号码',
            'legal_idcard_imgs' => '法人身份证上传：正面、反面、手持正面，|符号隔开',
            'com_name' => '企业名称',
            'com_tel' => '公司座机',
            'com_fax' => '公司传真',
            'com_address' => '公司地址',
            'com_city' => '公司所在地，省市区',
            'cus_like' => '客户满意度，百分比',
            'quality' => '品质合格率，百分比',
            'pact' => '合同履行率，百分比',
            'param' => '工厂额外参数',
            'out_type' => '业务类型，对接category的ID，公司109，工厂108，用||隔开',
            'years' => '公司年限 1=>1年内 2=>1-3年 3=>3-5年 5年',
            'age_range' => '年龄段几岁到几岁，中间英文逗号隔开',
            'detail' => '公司简介',
            'lat' => '地图坐标经度',
            'lng' => '地图坐标纬度',
            'created_at' => '',
            'updated_at' => '',
            'type' => '1表示公司信息  2表示工厂信息',
            'shenhe' => '审核',
            'visible' => '显示,隐藏',
        ];
    }

    /**
     * 获取企业的名称
     */
    public static function getCom_name($uid)
    {
        $val = self::where([
            'uid' => $uid,
            //    'type' => $type
        ])->select(['com_name'])->first();
        if ($val) {
            return $val->com_name;
        } else {
            return null;
        }
    }

    /**
     * 判断是否填写了基本信息
     */
    public static function infoexists($uid)
    {
        return self::where([
            'uid' => $uid
        ])->exists();
    }

    /**
     * 审核状态
     * $type=is_company  is_factory   is_yingpin
     */
    public static function getShenhe($uid)
    {
        $one = self::where([
            'uid' => $uid
        ])->select(['shenhe'])->first();
        if (!$one) {
            return self::shenhe_null;
        } else {
            return $one->shenhe;
        }
        /*
        if ($type == 'is_company') {
            $one = UserGongsiModel::where([
                'uid' => $uid
            ])->select(['shenhe'])->first();
            if (!$one) {
                return self::shenhe_null;
            } else {
                return $one->shenhe;
            }

        } elseif ($type == 'is_factory') {
             $one = UsergongchangModel::where([
                'uid' => $uid
            ])->select(['shenhe'])->first();
            if (!$one) {
                return self::shenhe_null;
            } else {
                return $one->shenhe;
            }

        } elseif ($type == 'is_yingpin') {

            return StaffModel::getshenhe($uid);
        } else {
            return false;
        }
       */
    }









     /**
     *  获取用户公司信息基本信息
     */
    public  function getgongshixinxi($uid)
    {
        $model=$this->where('uid',$uid)->first();
        if(!$model){
            return null;
        }
        $model->cus_like = $model->cus_like ? $model->cus_like : '';
        $model->quality = $model->quality ? $model->quality : '';
        $model->pact = $model->pact ? $model->pact : '';
        if ($model->age_range) {
            $ages = explode(',',$model->age_range);
            $model->ageStart = $ages[0]??'0';
            $model->ageEnd = $ages[1]??'0';
        } else {
            $model->ageStart = '';
            $model->ageEnd = '';
        }
        $model->outType = $model->out_type ? $model->out_type : '';
        if ($model->param) {
            $model->param = json_decode($model->param);
        }
        if ($model->param2) {
            $model->param2 = json_decode($model->param2);
        }

        return $model->toArray();
    }

    /**
     * 设置用户公司基本信息
    */
    public function setgongshixinxi($uid,$data){
            unset($data['cate']);
             unset($data['type']);
            UserQuick::removeCompanyQualified($uid);

        $model=$this->where('uid',$uid)->first();
        if(!$model){
            $data['created_at'] = time();
            $data['uid']=$uid;
          ///  print_r($data);exit;
            $id=$this->insertGetId($data);
             if($id){
                 return true;
             }
        }else{
             $data['facType']=(int)$data['facType'];
            $res=$this->where('uid',$uid)->update($data);
            if($res!==false){
                 return true;
            }
        }


    }




}